<?php
require_once dirname(__FILE__).'/../../homepage/lib/HomepageHelper.class.php';


class headerMenuComponents extends myComponents
{
	
  public function executeRender(){
  	$this->curNode = $this->getRoute()->getObject();

	$this->menu = NavigationMenu::getInstance(NavigationMenu::HEADER_MENU);
	

  }
  
  
  protected function getNodes( $recreate = false ){
		$q = $this->getMenuItemFetchingQuery();
		return $q->execute();
  }
  
  protected function getMenuItemFetchingQuery(){
  	return $this->setMenuItemContstraints(
				Doctrine::getTable('Structure')
				->createQuery('headerMenu')
				->select('s.*')
				->from('Structure s')
			);
  }
  
  protected function setMenuItemContstraints( Doctrine_Query $q ){
  	$q ->where('s.level > 1')
	  ->andWhere('s.lang = ?',  $this->getRoute()->getObject()->getLang())
	  ->andWhere('s.level <= ?', $this->getCurrentMenuLevel())
	  ->orderBy('s.lft asc')
	  ->setHydrationMode(Doctrine::HYDRATE_ARRAY);
	  
	  return $q;
  }
  
  protected function getCurrentMenuLevel(){
  	return 2;
  }
  
  public function executeLangSelect(){

	 $helper = new HomepageHelper();
	 $section = $this->getRoute()->getObject()->getParameter() == 'section' ? $this->getRoute()->getObject() :
			 $helper->getNodeSectionQuery($this->getRoute()->getObject())->execute()->getFirst();

	 if($section){
		$this->langs = Doctrine::getTable('Structure')->
			createQuery('all')
			->select('s.*')
			->from('Structure s')
			->where('s.isHidden = ?' , false)
			->andWhere('s.pageTitle = ?', $section->getPageTitle())
			->setHydrationMode(Doctrine::HYDRATE_RECORD)
			->execute();
	 }else{
		$this->langs = Doctrine::getTable('StructureLanguage')->
			createQuery('all')
			->select('sl.*')
			->from('StructureLanguage sl')
			->where('sl.isHidden = ?' , 0)
			->setHydrationMode(Doctrine::HYDRATE_RECORD)
			->execute();
	 }
		
	$this->currentNode = $this->getRoute()->getObject();
	
  }
  
  
  public function executeCurrencySelect(){
  	$this->currencies = Doctrine::getTable('Currency')->findAll();
  }
  
  
  public function executeSearchBox(){
  	
  }
  
  public function executeSignIn(){
  	$this->form = new SignInForm();
  	
  	$this->mySettings = Doctrine::getTable('Structure')->findOneByParameterAndLang('my settings', $this->getRoute()->getObject()->getLang());
  	$this->myOrders = Doctrine::getTable('Structure')->findOneByParameterAndLang('orders', $this->getRoute()->getObject()->getLang());
  	
  	$this->reminderForm = new PasswordReminderForm();
  }
  
  
  
  public function executeBreadCrumbs(){


	  $structNodes = array();
	  $c=0;
	  $el = NavigationMenu::getInstance(NavigationMenu::DEFAULT_MENU)->getElement(MenuElement::STRUCTURE_NODE,$this->getRoute()->getObject()->getId());
	  do{
		  $structNodes[$c] = $el->getFetchedData();
		  $structNodes[$c]['link'] = LinkGen::getInstance(LinkGen::STRUCTURE)->link($structNodes[$c]['id']);
		  ++$c;

		  $el = $el->getParentMenuElement();
	  }while($el != null);
	  $structNodes = array_reverse($structNodes);

//  	$structs = $this->getRoute()->getObject()->getNode()->getAncestors();
//  	$structs->add( $this->getRoute()->getObject() );
//  	$structNodes = array();
//
//  	# build structure breadscrumbs
//  	foreach($structs as $struct){
//  		$ar = $struct->toArray();
//  		$ar['link'] = LinkGen::getInstance(LinkGen::STRUCTURE)->link($this->getRoute()->getObject()->getId());
//  		$structNodes[] = $ar;
//
//  	}

  	# add category breadscrumbs
  	if($this->getRoute()->getCategoryObject()){
  		$cats = $this->getRoute()->getCategoryObject()->getNode()->getAncestors();
  		$cats->add($this->getRoute()->getCategoryObject());
  		
		  foreach($cats as $anc){
		  	if($anc['level'] == 0)continue; // root is not needed
		  	
		  	//prepare array
  			$ar = $anc->toArray();
  			$ar['title'] = $anc['name'];
  			$ar['link'] = LinkGen::getInstance(LinkGen::PRODUCT_GROUP)->link($this->getRoute()->getCategoryObject()->getId());
  			$ar['isHidden'] = 0;
  			
  			$structNodes[] = $ar;		  	
		  }
  	}
  	
  	
  	
  	# add product breadscrumb
  	if($this->getRoute()->getProductObject()){
  		$ar['link'] = ''; // @$this->getAction()->getComponent('linker', 'product', array('category' => $this->getRoute()->getCategoryObject(), 'product' => $this->getRoute()->getProductObject()) )
  		$ar['isHidden'] = 0;
  		$ar['title'] = $this->getRoute()->getProductObject()->getName(); 
  		
  		$structNodes[] = $ar;
  	}

	# add NewItem
	if($this->getRoute()->getNewsItemObject()){
  		$ar['link'] = ''; // @$this->getAction()->getComponent('linker', 'product', array('category' => $this->getRoute()->getCategoryObject(), 'product' => $this->getRoute()->getProductObject()) )
  		$ar['isHidden'] = 0;
  		$ar['title'] = $this->getRoute()->getNewsItemObject()->getName();

  		$structNodes[] = $ar;
	}
  	
  	$this->nodes = $structNodes;

  }
  
  
}